package net.qhzw.geologr5.gis.service.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.geologr5.gis.common.framework.utils.JsonResult;
import net.qhzw.geologr5.gis.common.framework.utils.StringUtils;
import net.qhzw.geologr5.gis.service.system.component.FileTemplate;
import net.qhzw.geologr5.gis.service.system.entity.DownloadSpecialty;
import net.qhzw.geologr5.gis.service.system.entity.Standard;
import net.qhzw.geologr5.gis.service.system.mapper.DownloadSpecialtyMapper;
import net.qhzw.geologr5.gis.service.system.mapper.StandardMapper;
import net.qhzw.geologr5.gis.service.system.query.StandardQuery;
import net.qhzw.geologr5.gis.service.system.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service
@Slf4j
public class StandardServiceImpl implements StandardService {

    @Autowired
    private StandardMapper standardMapper;

    @Autowired
    private FileTemplate fileTemplate;

    @Autowired
    private DownloadSpecialtyMapper downloadSpecialtyMapper; //专业类别dao

    /***
     * 查询规范列表
     * @param query
     * @return
     */
    @Override
    public JsonResult findStandard(StandardQuery query) {
        Map<String, Object> result = new HashMap<>();
        PageHelper.startPage(query.getPage(), query.getLimit());
        try {
            List<Standard> standardList = standardMapper.findStandard(query);
            PageInfo<Standard> pageInfo = new PageInfo<>(standardList);
            result.put("list", pageInfo.getList());
            result.put("total", pageInfo.getTotal());
            return JsonResult.success(result);
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 创建规范列表
     * @param standard
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult createStandard(Standard standard) {
        try {
            if (standard == null) {
                return JsonResult.error("参数为空");
            }
            if (standard.getId() != null && standard.getId() > 0) {
                int count = standardMapper.updateById(standard);
                if (count > 0) {
                    return JsonResult.success("修改成功");
                } else {
                    return JsonResult.error("修改失败");
                }
            }
            if (standard.getCreateDate() == null) {
                standard.setCreateDate(new Date());
            }
            int count = standardMapper.insert(standard);
            if (count > 0) {
                return JsonResult.success("创建成功");
            } else {
                return JsonResult.error("创建失败");
            }
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 删除规范表
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public JsonResult deleteByIds(Integer[] ids) {
        try {
            if (StringUtils.isNull(ids)) {
                return JsonResult.error("记录ID不能为空");
            }
            List<Integer> idList = Arrays.asList(ids);
            int count = standardMapper.deleteBatchIds(idList);
            if (count <= 0) {
                return JsonResult.error("删除失败");
            }
            return JsonResult.success();
        } catch (Exception e) {
            return JsonResult.error(e.getMessage());
        }
    }


    /****
     * 上传规范
     * @param standard
     * @param file
     * @return
     */
    @Override
    public JsonResult uploadStandard(Standard standard, MultipartFile file) {
        log.info("上传规范==StandardServiceImpl==method:uploadStandard==param: {}, ", JSON.toJSONString(standard));
        log.info("上传规范==StandardServiceImpl==method:uploadStandard==fileName{}, fielSize: {}, ", file.getOriginalFilename(), file.getSize());
        try {
            String fileUrl = fileTemplate.fileUpload(file);
            standard.setCreateDate(new Date());
            standard.setFileUrl(fileUrl);
            int count = standardMapper.insert(standard);
            if (count > 0) {
                return JsonResult.success();
            }
            return JsonResult.error();
        } catch (Exception e) {
            log.info("上传规范失败==StandardServiceImpl==method:uploadStandard==errMsg: {}, ", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


    /***
     * 查询类别
     * @return
     */
    @Override
    public JsonResult getSpecialty() {
        try {
            List<DownloadSpecialty> specialtiesList = downloadSpecialtyMapper.selectList(null);
            return JsonResult.success(specialtiesList);
        } catch (Exception e) {
            log.error("查询类别失败==StandardServiceImpl==method:getSpecialty==errMsg： {}", e.getMessage());
            return JsonResult.error(e.getMessage());
        }
    }


}
